package com.ruoyi.driver.carservicestatics.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.driver.carservicestatics.mapper.DriverCarServiceStaticsMapper;
import com.ruoyi.driver.carservicestatics.ro.*;
import com.ruoyi.driver.carservicestatics.service.DriverCarServiceStaticsService;
import com.ruoyi.driver.carservicestatics.vo.*;
import com.ruoyi.management.basics.service.ITCompanyService;
import com.ruoyi.management.basics.service.ITEmployeeService;
import com.ruoyi.management.basics.vo.GetDriverDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 驾驶端运营管理
 * 
 * @author xly
 * @date 2021-12-08
 */
@Service
public class DriverCarServiceStaticsServiceImpl implements DriverCarServiceStaticsService
{
    @Autowired
    private DriverCarServiceStaticsMapper driverCarServiceStaticsMapper;
    @Autowired
    private ITEmployeeService employeeService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GetDriverCarServiceStaticsDriverVo listDirverDetail(GetDriverCarServiceStaticsListDriverParam param,String userName) {
        if(StringUtils.isEmpty(userName)){
            throw new ServiceException("获取当前用户失败，请稍后重试");
        }
        GetDriverDetailVo user = employeeService.getEmployeeDetailByPhone(userName);
        if(null == user){
            throw new ServiceException("获取当前用户失败，请稍后重试");
        }
        if(null == user.getId()){
            throw new ServiceException("获取当前用户失败，请稍后重试");
        }
        param.setEmployeeId(user.getId());
        GetDriverCarServiceStaticsDriverVo vo = driverCarServiceStaticsMapper.listDirver(param);
        List<GetDriverCarServiceStaticsDriverDetailVo> detailList = new ArrayList<>();
        List<String> month = new ArrayList<>(
                Arrays.asList("01","02","03","04","05","06","07","08","09","10",
                        "11","12"));
        if (null != vo && null != vo.getMonthDays() && !"".equals(vo.getMonthDays()) && null != vo.getMonthCounts() && !"".equals(vo.getMonthCounts())) {
            String[] days = vo.getMonthDays().split(",");
            String[] count = vo.getMonthCounts().split(",");
            for (String monthDay : days) {
                month.remove(monthDay.substring(0, 2));
                for (String monthCounts : count) {
                    boolean dayBoolean1 = "01".equals(monthDay.substring(0, 2));
                    boolean dayBoolean2 = "02".equals(monthDay.substring(0, 2));
                    boolean dayBoolean3 = "03".equals(monthDay.substring(0, 2));
                    boolean dayBoolean4 = "04".equals(monthDay.substring(0, 2));
                    boolean dayBoolean5 = "05".equals(monthDay.substring(0, 2));
                    boolean dayBoolean6 = "06".equals(monthDay.substring(0, 2));
                    boolean dayBoolean7 = "07".equals(monthDay.substring(0, 2));
                    boolean dayBoolean8 = "08".equals(monthDay.substring(0, 2));
                    boolean dayBoolean9 = "09".equals(monthDay.substring(0, 2));
                    boolean dayBoolean10 = "10".equals(monthDay.substring(0, 2));
                    boolean dayBoolean11 = "11".equals(monthDay.substring(0, 2));
                    boolean dayBoolean12 = "12".equals(monthDay.substring(0, 2));
                    boolean countBoolean1 = "01".equals(monthCounts.substring(0, 2));
                    boolean countBoolean2 = "02".equals(monthCounts.substring(0, 2));
                    boolean countBoolean3 = "03".equals(monthCounts.substring(0, 2));
                    boolean countBoolean4 = "04".equals(monthCounts.substring(0, 2));
                    boolean countBoolean5 = "05".equals(monthCounts.substring(0, 2));
                    boolean countBoolean6 = "06".equals(monthCounts.substring(0, 2));
                    boolean countBoolean7 = "07".equals(monthCounts.substring(0, 2));
                    boolean countBoolean8 = "08".equals(monthCounts.substring(0, 2));
                    boolean countBoolean9 = "09".equals(monthCounts.substring(0, 2));
                    boolean countBoolean10 = "10".equals(monthCounts.substring(0, 2));
                    boolean countBoolean11 = "11".equals(monthCounts.substring(0, 2));
                    boolean countBoolean12 = "12".equals(monthCounts.substring(0, 2));
                    int day = Integer.parseInt(monthDay.substring(3, monthDay.length()));
                    int num = Integer.parseInt(monthCounts.substring(3, monthCounts.length()));
                    if (dayBoolean1 && countBoolean1) {
                        detailList.add(newVo(1, day, num));
                    } else if (dayBoolean2 && countBoolean2) {
                        detailList.add(newVo(2, day, num));
                    } else if (dayBoolean3 && countBoolean3) {
                        detailList.add(newVo(3, day, num));
                    } else if (dayBoolean4 && countBoolean4) {
                        detailList.add(newVo(4, day, num));
                    } else if (dayBoolean5 && countBoolean5) {
                        detailList.add(newVo(5, day, num));
                    } else if (dayBoolean6 && countBoolean6) {
                        detailList.add(newVo(6, day, num));
                    } else if (dayBoolean7 && countBoolean7) {
                        detailList.add(newVo(7, day, num));
                    } else if (dayBoolean8 && countBoolean8) {
                        detailList.add(newVo(8, day, num));
                    } else if (dayBoolean9 && countBoolean9) {
                        detailList.add(newVo(9, day, num));
                    } else if (dayBoolean10 && countBoolean10) {
                        detailList.add(newVo(10, day, num));
                    } else if (dayBoolean11 && countBoolean11) {
                        detailList.add(newVo(11, day, num));
                    } else if (dayBoolean12 && countBoolean12) {
                        detailList.add(newVo(12, day, num));
                    }
                }
            }
        }
        for (String s : month) {
            detailList.add(newVo(Integer.valueOf(s), 0, 0));
        }
        if(null != vo){
            vo.setDetailList(detailList);
            vo.getDetailList().sort(Comparator.comparing(GetDriverCarServiceStaticsDriverDetailVo::getMonth));
            return vo;
        }
        GetDriverCarServiceStaticsDriverVo driverVo = new GetDriverCarServiceStaticsDriverVo();
        driverVo.setDetailList(detailList);
        driverVo.getDetailList().sort(Comparator.comparing(GetDriverCarServiceStaticsDriverDetailVo::getMonth));
        return driverVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GetDriverCarServiceStaticsDriverVo listDirver(GetDriverCarServiceStaticsListDriverParam param, String userName) {
        if(StringUtils.isEmpty(userName)){
            throw new ServiceException("获取当前用户失败，请稍后重试");
        }
        GetDriverDetailVo user = employeeService.getEmployeeDetailByPhone(userName);
        if(null == user){
            throw new ServiceException("获取当前用户失败，请稍后重试");
        }
        if(null == user.getId()){
            throw new ServiceException("获取当前用户失败，请稍后重试");
        }
        param.setEmployeeId(user.getId());
        GetDriverCarServiceStaticsDriverVo vo = driverCarServiceStaticsMapper.listDirver(param);
        if(null == vo){
            GetDriverCarServiceStaticsDriverVo driverVo = new GetDriverCarServiceStaticsDriverVo();
            driverVo.setOperateDays(0);
            driverVo.setOperateNumbers(0);
            driverVo.setEmployeeName(user.getName());
            driverVo.setYear(param.getYear());
            return driverVo;
        }
        return vo;
    }

    public GetDriverCarServiceStaticsDriverDetailVo newVo(int month,int day,int num){
        GetDriverCarServiceStaticsDriverDetailVo detail = new GetDriverCarServiceStaticsDriverDetailVo();
        detail.setMonth(month);
        detail.setDays(day);
        detail.setNumbers(num);
        return detail;
    }
}
